﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Threading;

namespace System.Web.UI
{
    public class Base : System.Web.UI.Page
    {
        protected string pageClassName;
        protected Hashtable userFunctions;
        private string requestString;
        private bool IsLoad = true;

        /// <summary>
        /// 接收页面参数索引
        /// </summary>
        /// <param name="key">参数名称</param>
        /// <returns>返回处理非法字符后的值</returns>
        public string this[string key]
        {
            get
            {
                if (Request.QueryString[key] != null)
                {
                    requestString = StringHelper.SafeRequest(Request.QueryString[key]);
                }
                return Request[key];
            }
        }

        public Base()
        {
        }


        /// <summary>
        /// 该方式一般情况下不要重写
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {

            CheckLogin();
            CheckRights();

            if (IsLoad)
            {
                InitializeComponent();
                base.OnLoad(e);
            }

        }

        protected override void OnError(EventArgs e)
        {
            Exception ex = Server.GetLastError();

            string error = LogException(ex);

            Server.ClearError();
            //Thread.Sleep(ZEAppSet.SleepTimes);
            Response.Redirect(error, true);

            base.OnError(e);
        }

        private string LogException(Exception ex)
        {
            string error = string.Format("URl：{0}\n引发异常的方法：{1}\n错误信息：{2}\n错误堆栈：{3}\n", this.Request.RawUrl, ex.TargetSite, ex.Message, ex.StackTrace);
            string errorUrl = string.Format("{0}?RawUrl={1}&TargetSite={2}&Message={3}", Page.ResolveUrl("~/Error.aspx"), this.Request.RawUrl, ex.TargetSite, ex.Message);

            //用户异常记录日志并弹出提示,系统异常仅记录日志
            System.Diagnostics.EventLogEntryType type = System.Diagnostics.EventLogEntryType.Error;

            if (ex is MyException)
            {
                type = System.Diagnostics.EventLogEntryType.Warning;
            }

            //LogWrite.WriteLog(error, type);

            return errorUrl;
        }

        internal virtual void InitializeComponent()
        {
        }

        /// <summary>
        /// 页面访问权限控制
        /// </summary>
        protected virtual void CheckLogin()
        {
            //if (Session["ID"] == null)
            //{
            //    IsLoad = false;
            //    RedirectLogin();
            //}
        }

        /// <summary>
        /// 控制权限,请重写该方法以实现权限控制
        /// </summary>
        protected virtual void CheckRights()
        {
            //RedirectLogin();
        }

        protected virtual void RedirectLogin()
        {
            string url = Page.ResolveUrl("~/ZhiMa.aspx");
            Page.Response.Write("<script language=javascript>alert('请重新登录!');window.open('" + url + "','_top','');</script>");
        }

        protected Base RequestHelper
        {
            get
            {
                return this;
            }
        }
    }
}
